//============================================================================
// Name        : t_log.cpp
// Author      : inkooboo
// Version     :
// Copyright   :
// Description : main in C++
//============================================================================

#include "systems/logger.hpp"

#include <OgreRoot.h>

#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/scope_exit.hpp>
#include <boost/timer.hpp>

#include <vector>
#include <stdio.h>

using namespace std;

static const int thr_number = 10;
static const int speed_log_number = 10000;

void thr_func_speed(int n) {

  char tmp[256];


  sprintf(tmp, "%d", n);

  logger::thr_desc_t name_thr(tmp);

  logger::log(TRACE) << "[Stupid thread] Thread started";

  for (int i = 0; i < speed_log_number; ++i) {
      logger::log(DEBUG) << "[Stupid thread] Log in speed";
      //logger::log(DEBUG, "[Stupid thread] Log in speed");
  }

}

std::vector<boost::shared_ptr<boost::thread> > threads;

int main(void) {

  logger::logger_t logger("my_log.txt");

  //test ogre logger
  logger::log(TRACE) << "[Main] Begin create and destroy ogre root test";

  {
    boost::scoped_ptr<Ogre::Root> root(new Ogre::Root("",""));
    boost::this_thread::sleep(boost::posix_time::milliseconds(rand()%1000));
  }

  logger::log(TRACE) << "[Main] End ogre root test";

  //test speed and thread safety
  logger::log(TRACE) << "[Main] Begin logger speed test";

  boost::timer timer;

  for (int i = 0; i < thr_number; ++i) {
      boost::shared_ptr<boost::thread> thread(new boost::thread(&thr_func_speed, i));
      threads.push_back(thread);
  }

  for(int i = 0; i < thr_number; ++i) {
    threads[i]->join();
  }

  logger::log(TRACE) << "[Main] Created " << thr_number << " threads logged " << speed_log_number*thr_number << " messages via " << timer.elapsed() << " sec in "
# ifdef _DEBUG
    "Debug"
# else
    "Release"
# endif
    " build";

  logger::log(TRACE) << "[Main] End logger speed test";

  return 0;
}
